<?php
/**
*	系统里默认的menu模块的
*/
namespace Rainrock\Framework\Module;


use Rainrock\Framework\kernel\core\ModuleInfo;
use Rainrock\Framework\kernel\core\ModuleFields;
use Rainrock\Framework\kernel\base\Rock;

class SysmenuModuleInfo extends ModuleInfo{
	
	protected function initModule(){
		 $this->setName('系统菜单');
		 $this->setNum('sysmenu');

		 $this->addFields(ModuleFields::get('icons')
			->setName('图标')
			->setLen(300)
			->setSm('可用写图片路径或文字图标')
			->setType('varchar')
		 );
		 
		 $this->addFields(ModuleFields::get('name')
			->setName('菜单名称')
			->setIsbt(1)
			->setLen(30)
			->setAlign('left')
			->setWidth(300)
			->setType('varchar')
		 );
		 
		 $this->addFields(ModuleFields::get('url')
			->setName('对应地址')
			->setLen(300)
			->setAlign('left')
			->setSm('格式：控制器,方法,参数')
			->setType('varchar')
		 );
		 
		 $this->addFields(ModuleFields::get('num')
			->setName('编号')
			->setLen(30)
			->setSm('字母数字组合')
			->setType('varchar')
		 );
		 
		 $this->addFields(ModuleFields::get('pid')
			->setName('上级ID')
			->setDev(0)
			->setType('int')
		 );
		

		 $this->addFields(ModuleFields::get('status')
			->setName('状态')
			->setLen(1)
			->setDev(1)
			->setSm('启用菜单')
			->setIseditlx(1)
			->setIspx(1)
			->setFieldstype('checkbox')
			->setType('tinyint')
		 );
		 
		 $this->addFields(ModuleFields::get('type')
			->setName('类型')
			->setLen(1)
			->setIspx(1)
			->setDev(0)
			->setData('0|全部人可用,1|管理员可用|blue,2|仅超管可用|red')
			->setFieldstype('select')
			->setType('tinyint')
		 );
		 
		 $this->addFields(ModuleFields::get('recename')
			->setName('可用人员')
			->setLen(300)
			->setType('varchar')
		 );

		 $this->addFields(ModuleFields::get('receid')
			->setName('可用人员ID')
			->setIslb(0)
			->setLen(300)
			->setType('varchar')
		 );
		 
		 $this->addFields(ModuleFields::get('isopen')
			->setName('展开')
			->setLen(1)
			->setDev(0)
			->setSm('展开菜单')
			->setIseditlx(1)
			->setFieldstype('checkbox')
			->setType('tinyint')
		 );
		 
		 $this->addFields(ModuleFields::get('sort')
			->setName('排序号')
			->setDev(0)
			->setIseditlx(1)
			->setIspx(1)
			->setType('int')
		 );
	}
	
	/**
	*	添加默认菜单
	*/
	protected function initFirst()
	{
		$menusub[] = array(
			'name'  => '系统设置',
			'url'	=> 'adminmanage,cog',
			'num'	=> 'syscog',
			'type'  => 2,
		);
		$menusub[] = array(
			'name'  => '登录用户',
			'url'	=> 'input,list,num=users',
			'num'	=> 'users',
			'type'  => 2,
		);
		$menusub[] = array(
			'name'  => '菜单管理',
			'url'	=> 'input,list,num=sysmenu',
			'num'	=> 'sysmenu',
			'type'  => 2,
		);
		$menusub[] = array(
			'name'  => '登录记录',
			'url'	=> 'input,list,num=systoken',
			'num'	=> 'systoken',
			'type'  => 2,
		);
		$menusub[] = array(
			'name'  => '系统日志',
			'url'	=> 'input,list,num=syslog',
			'num'	=> 'syslog',
			'type'  => 2,
		);
		$menusub[] = array(
			'name'  => '计划任务',
			'url'	=> 'input,list,num=systask',
			'num'	=> 'systask',
			'type'  => 2,
		);
		$menusub[] = array(
			'name'  => '系统文件',
			'url'	=> 'adminmanage,filelist',
			'num'	=> 'filelist',
			'type'  => 2,
		);
		$menusub[] = array(
			'name'  => '模块更新',
			'url'	=> 'adminmanage,modeupdate',
			'num'	=> 'modeupdate',
			'type'  => 2,
		);
		$arr[] = array(
			'pid'  => 0,
			'name' => '系统',
			'type' => 2,
			'isopen' => 1,
			'sort' => 100,
			'menusub' => $menusub
		);
		$arr[] = array(
			'pid'  => 0,
			'name' => '数据库管理',
			'url'	=> 'datamanage,table',
			'num'	=> 'dbmanage',
			'sort' => 101,
			'type' => 2,
		);
		foreach($arr as $k=>$rs){
			$this->addMenu($rs['pid'], $rs['name'], $rs, $k);
		}
	}
	
	public function addMenu($pid, $name, $rs=array(), $sort=0)
	{
		$where 	= "`pid`='$pid' AND `name`='$name'";
		if(isset($rs['num']))$where  = "`pid`='$pid' AND `num`='".$rs['num']."'";
		$ors 	= $this->model->getone($where);
		$menusub = isset($rs['menusub']) ? $rs['menusub'] : false;
		unset($rs['menusub']);
		if(!isset($rs['sort']))$rs['sort'] = $sort;
		$rs['name'] = $name;
		if(!$ors){
			$rs['pid'] = $pid;
			$nid = $this->model->insert($rs);
		}else{
			$nid = $ors['id'];
			unset($rs['isopen']);
			$this->model->update($rs, $nid);
		}
		if($menusub)foreach($menusub as $k1=>$rs1){
			$this->addMenu($nid, $rs1['name'], $rs1, $k1);
		}
	}
	
}